package com.glory.brepda.activity.lanjian;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.brepda.activity.mfg.LotShelvesActivity;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.R;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import butterknife.BindView;
import butterknife.OnClick;

public class DJLotShelvesActivity extends BaseActivity {

    private FreeUI_GeneralPop codePop;
    private ArrayList<String> djTypeList = new ArrayList<>(Arrays.asList("T停测区", "D待处理良品","F滞留不良品"));
    private  LotInfoEntity lotInfoEntity;
    private int sbsbsb=-1;

    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvStepdesc)
    TextView tvStepdesc;
    @BindView(R.id.tvMainqty)
    TextView tvMainqty;
    @BindView(R.id.tvproductmodule)
    TextView tvproductmodule;
    @BindView(R.id.etShelves)
    FreeUI_ClearEditText etShelves;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvDJType)
    TextView tvDJType;
    @BindView(R.id.ivDJTypeArrow)
    ImageView ivDJTypeArrow;
    @BindView(R.id.flDJTypeList)
    FrameLayout flDJTypeList;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_djlot_shelves;
    }

    @Override
    protected void initView() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);

        flDJTypeList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (codePop != null) {
                    if (codePop.isShowing()) {
                        codePop.dismiss();
                    }
                } else {
                    codePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flDJTypeList.getWidth(), djTypeList, ivDJTypeArrow, (ty, i) -> {
                        tvDJType.setText(ty);
                        sbsbsb=i;
                        switch (i){
                            case 0:
                            case 1:
                                break;
                            case 2:
//                                Log.d("推荐货架路线F","推荐货架路线F");
                                if(getText(etLotID).equals("")){
                                    return;
                                }
                                submitF();
                        }
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivDJTypeArrow);
                codePop.showAsDropDown(flDJTypeList, 0, 0);
            }
        });

        initEdit(etLotID, s -> {

            FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                    ("正在查询LOT信息", new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplication(), true, getSupportFragmentManager())) {

                        lotInfoEntity = response.body().getResponse().getBody();
                        tvStepdesc.setText(lotInfoEntity.getLOT().getSTEPDESC());
                        tvMainqty.setText(lotInfoEntity.getLOT().getMAINQTY());
                        etShelves.setText(lotInfoEntity.getLOCATORID());
                        tvproductmodule.setText(lotInfoEntity.getLOT().getUDF().getProductModel());
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onError(response);
                    loadError(response.getException(), "LOTQUERY");
                }
            };

            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "TESTBANKLOTLOCATORINFOBYID");
            map.addProperty("DJTYPE","");
            map.addProperty("LOTID", s);
            NetUtils.requestPostNet("IF.LOTQUERY", "Common", getApplication(), map, callBack);
        });
    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view){
            switch (view.getId()){
                case R.id.tvBaseSubmit:
                    if(getText(etLotID).equals("") || getText(etShelves).equals("")){
                        showToast("批次号或者货架号不能为空！",false);
                        return;
                    }
                    if(!lotInfoEntity.getLOT().getSTEPDESC().equals("测编工序异常分析")){
                        showToast("待处理货架只能批次号在待处理工步(测编工序异常分析)上货架！",false);
                        return;
                    }
                    if(lotInfoEntity.getLOT().getMAINQTY().equals("0")){
                        showToast("批次号数量为0不允许上货架！",false);
                        return;
                    }
                    if(tvDJType.getText().equals("")){
                        showToast("请选择上架类型！",false);
                        return;
                    }
                    if(sbsbsb != -1){
                        String hj = getText(etShelves);
                        boolean ok = false;
                        if(sbsbsb==0 && hj.contains("T")){
                            ok = true;
                        }else if(sbsbsb==1 && hj.contains("D")){
                            ok = true;
                        }else if(sbsbsb==2 && hj.contains("F")){
                            ok = true;
                        }
                        if(!ok){
                            showToast("选择的上架类型与货架类型不匹配!！",false);
                            return;
                        }
                    }

                    showSubmitDialog(new onSubmitListener() {
                        @Override
                        public void setOnSubmitListener() {
                            FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                                    (new TypeToken<BaseResponseBean<JSONObject>>() {
                                    }.getType(), getSupportFragmentManager(), this) {

                                @Override
                                public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                                    super.onSuccess(response);
                                    if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                                        showToast("上架成功", true);
                                        etLotID.setText("");
                                        tvStepdesc.setText("");
                                        tvMainqty.setText("");
                                        etShelves.setText("");
                                        tvproductmodule.setText("");
                                        tvDJType.setText("");
                                        sbsbsb=-1;
                                    }
                                }

                                @Override
                                public void onError(Response<BaseResponseBean<JSONObject>> response) {
                                    super.onError(response);
                                    loadError(response.getException(),"CPCMOVELOCATOR");
                                }
                            };
                            JsonObject map = new JsonObject();
                            map.addProperty("ACTIONTYPE", "BIND");
                            map.addProperty("LOTID", getText(etLotID));
                            map.addProperty("LOCATORID", getText(etShelves));
                            NetUtils.requestPostNet("IF.BREMOVELOCATOR", "Common", getApplication(), map, callBack);
                        }
                    });
                    break;
            }
        }

    private void submitF(){

        FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                (new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    etShelves.setText(response.body().getResponse().getBody().getLOCATORID());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "LOTQUERY");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "TESTBANKLOTLOCATORINFOBYID");
        map.addProperty("LOTID", getText(etLotID));
        map.addProperty("DJTYPE","F");
        NetUtils.requestPostNet("IF.LOTQUERY", "Common", getApplication(), map, callBack);
    }

    @Override
    protected void initData() {

    }
}